<?php
namespace app\admin\controller;
use app\admin\Controller;
use app\admin\model\Mall as MallModel;
use pidan\facade\Request;
use pidan\facade\Db;
use pidan\facade\Validate;

class Mall extends Controller
{
	protected function initialize()
	{
		$this->model=new MallModel;
	}
	public function list(){
		$req=$this->request;
		$table=$this->model;
		//要显示的
		$filed=explode(',','id,name,url,logo');

		$sort_by=input('request.sort_by','id','trim');
		$sort_order=input('request.sort_order','asc','trim');

		$page=input('request.page',1,'intval');
		$limit=input('request.limit',30,'intval');

		$list= $table->field($filed);

		$count=$list->count('id');
		$list=$list->page($page)->order($sort_by, $sort_order)->limit($limit)->select()->toArray();
		return $this->lists($list,$count);
	}
	public function add(){
		$table=$this->model;
		$max=$table->count('id')+1;
		$table->save(['name'=>'name'.$max]);
		$result=['code'=>0,'msg'=>''];
		return $this->result($result);
	}
	public function edit(){
		$id=input('request.id',0,'intval');
		$field=input('request.field','','trim');
		$value=input('request.value',0,'trim');
		try {
			$table = $this->model->find($id);
			$table->save([$field => $value]);
			$result=['code'=>0,'msg'=>''];
		} catch (\Exception $e) {
			$result=['code'=>1,'msg'=>$e->getMessage()];
		}

		return $this->result($result);
	}

}
